package com.hz.ruoyilgorder.api.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hz.rouyigonggong.domain.LgDeliveryInfo;
import com.hz.rouyigonggong.domain.LgInboundOrder;
import com.hz.rouyigonggong.domain.LgOutOrder;
import com.hz.rouyigonggong.domain.LgSellerInfo;
import com.hz.rouyigonggong.domain.dto.LgInboundOrderDTO;
import com.hz.rouyigonggong.domain.dto.LgOutOrderDTO;
import com.hz.ruoyilgorder.api.service.LgSellerInfoService;
import com.hz.ruoyilgorder.config.LgInboundOrderConverter;
import com.hz.ruoyilgorder.config.LgOutOrderConverter;
import com.hz.ruoyilgorder.mapper.LgInboundOrderDTOMapper;
import com.hz.ruoyilgorder.service.LgInboundOrderService;
import com.hz.ruoyilgorder.service.LgOutOrderService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;

/**
 * @version 1.0
 * @Author yzw
 * @Date 2025/3/13 19:55
 * @注释 白 jia
 */
@RestController
@RequestMapping("/api")
public class LgInOrder {
    @Autowired
    private LgInboundOrderService lgInboundOrderService;

    @Autowired
    private LgInboundOrderDTOMapper lgInboundOrderDTOMapper;
    @Autowired
    private LgOutOrderService lgOutOrderService;
    @Autowired
    private LgSellerInfoService lgSellerInfoService;

    @PostMapping("/edit")
    //入库订单修改状态
    public AjaxResult edit(@RequestBody LgInboundOrderDTO order){
        LgInboundOrder entity = LgInboundOrderConverter.toEntity(order);
        if(entity.getStatus()==4){
            LgSellerInfo lgSellerInfo = new LgSellerInfo();
            lgSellerInfo.setInboundOrderNumber(entity.getOrderNumber());
            lgSellerInfo.setRemainingQuantity(entity.getActualQuantity());
            lgSellerInfo.setRemainingWeight(entity.getVerifiedWeight().intValue());
            lgSellerInfoService.insertSellerInfo(lgSellerInfo);
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("order_number",entity.getOrderNumber());
        return AjaxResult.success(lgInboundOrderService.update(order, updateWrapper));
    };

    //出库订单修改状态
    @PostMapping("/editoutorder")
    public AjaxResult editoutorder(@Validated @RequestBody LgOutOrderDTO orderDTO) {
        LgOutOrder order = LgOutOrderConverter.toEntity(orderDTO);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("stock_order_number",order.getStockOrderNumber());

        return AjaxResult.success(lgOutOrderService.update(order, updateWrapper));
    }










    @GetMapping(value = "/getInfo")
    public LgInboundOrderDTO getInfo(@RequestParam("orderNumber") Integer orderNumber) {

        LgInboundOrderDTO lgInboundOrderDTO = lgInboundOrderDTOMapper.getOrder(orderNumber);
        return lgInboundOrderDTO;
    }

}
